package com.kinon.markett2.tools;

import android.content.Context;
import android.util.Log;

import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.RequestParams;

public class HttpTools {
    Context context;
    AsyncHttpClient client = new AsyncHttpClient();

    public HttpTools(Context context) {
        super();
        this.context = context;
        client.setTimeout(10 * 1000);
    }

    public HttpTools(Context context, int outTime) {
        super();
        client.setTimeout(outTime);
        this.context = context;
    }

    /**
     * 获得后面请求的地址
     */
    public void getByMachine(String sn, JsonHttpResponseHandler backcall) {
        RequestParams params = new RequestParams();
        params.put("machine_code", sn);
        Log.e("lh", "url==" + Url.serverGetUrl + Url.getByMachine);
        client.post(context, Url.serverGetUrl + Url.getByMachine, params, backcall);
    }

    /**
     * 登录请求
     */
    public void login(String phone, String password, String type,
                      JsonHttpResponseHandler backcall) {
        RequestParams params = new RequestParams();
        params.put("username", phone);
        params.put("password", password);
        params.put("type", type);
        Log.e("lh", "url==" + Url.ServerUrlQT + Url.loginUrl);
        client.post(context, Url.ServerUrlQT + Url.loginUrl, params, backcall);
    }


    /**
     * 查询商品
     */
    public void bycode(String token, String code,
                       JsonHttpResponseHandler backcall) {
        RequestParams params = new RequestParams();
        params.put("token", token);
        params.put("code", code);
        Log.e("lh", "url==" + Url.ServerUrl + Url.bycode);
        client.post(context, Url.ServerUrl + Url.bycode, params, backcall);
    }

    /**
     * 收银商品信息保存
     */
    public void shouyinsave(String token, String goods, String note, String inventory_money,
                            JsonHttpResponseHandler backcall) {
        RequestParams params = new RequestParams();
        params.put("token", token);
        params.put("goods", goods);
        params.put("note", note);
        params.put("inventory_money", inventory_money);
        Log.e("lh", "url==" + Url.ServerUrl + Url.shouyinsave);
        client.post(context, Url.ServerUrl + Url.shouyinsave, params, backcall);
    }

    /**
     * 收银商品信息提交
     */
    public void shouyinconfirm(String token, String id, String usercode,
                               JsonHttpResponseHandler backcall) {
        RequestParams params = new RequestParams();
        params.put("token", token);
        params.put("id", id);
        params.put("usercode", usercode);
        Log.e("lh", "url==" + Url.ServerUrl + Url.shouyinconfirm);
        client.post(context, Url.ServerUrl + Url.shouyinconfirm, params, backcall);
    }

    /**
     * 获取供应商
     */
    public void getSuppliers(String token,
                             JsonHttpResponseHandler backcall) {
        RequestParams params = new RequestParams();
        params.put("token", token);
        Log.e("lh", "url==" + Url.ServerUrl + Url.getSuppliers);
//		client.post(context, Url.getSuppliers, params, backcall);
        client.post(context, Url.ServerUrl + Url.getSource, params, backcall);
    }

    /**
     * 入库保存
     */
    public void rukusave(String token, String goods, String supplier_id, String payment_type, String note,
                         JsonHttpResponseHandler backcall) {
        RequestParams params = new RequestParams();
        params.put("token", token);
        params.put("goods", goods);
//		params.put("supplier_id", supplier_id);
        params.put("payment_type", payment_type);
        params.put("note", note);
        params.put("source", supplier_id);
        Log.e("lh", "url==" + Url.ServerUrl + Url.rukusave);
        client.post(context, Url.ServerUrl + Url.rukusave, params, backcall);
    }

    /**
     * 入库提交
     */
    public void rukuconfirm(String token, String id,
                            JsonHttpResponseHandler backcall) {
        RequestParams params = new RequestParams();
        params.put("token", token);
        params.put("id", id);
        Log.e("lh", "url==" + Url.ServerUrl + Url.rukuconfirm);
        client.post(context, Url.ServerUrl + Url.rukuconfirm, params, backcall);
    }

    /**
     * 获取订单列表
     */
    public void getOrders(String token, String page, String offset,
                          JsonHttpResponseHandler backcall) {
        RequestParams params = new RequestParams();
        params.put("token", token);
        params.put("page", page);
        params.put("offset", offset);
        Log.w("WZY", "token : " + token);
        Log.e("lh", "url==" + Url.ServerUrl + Url.order);
        client.post(context, Url.ServerUrl + Url.order, params, backcall);
    }

    /**
     * 订单列表
     */
    public void getDyOrders(String token, String page, String offset, String type,
                            JsonHttpResponseHandler backcall) {
        RequestParams params = new RequestParams();
        params.put("token", token);
        params.put("page", page);
        params.put("offset", offset);
        params.put("type", type);
        Log.e("WZY", "url==" + Url.ServerUrlQT);
        Log.e("lh", "url==" + Url.getOrders);
        client.post(context, Url.ServerUrlQT + Url.getOrders, params, backcall);
    }

    /**
     * 打印
     */
    public void printOrder(String token, String orderid,
                           JsonHttpResponseHandler backcall) {
        RequestParams params = new RequestParams();
        params.put("token", token);
        params.put("orderid", orderid);
        Log.e("lh", "url==" + Url.printOrder);
        client.post(context, Url.ServerUrlQT + Url.printOrder, params, backcall);
    }

    /**
     * 打印的订单详情
     */
    public void getDyDetail(String token, String orderno, String dayno, String orderid,
                            JsonHttpResponseHandler backcall) {
        RequestParams params = new RequestParams();
        params.put("token", token);
        params.put("orderno", orderno);
        params.put("dayno", dayno);
        params.put("orderid", orderid);
        Log.e("lh", "url==" + Url.getDetail);
        client.post(context, Url.ServerUrlQT + Url.getDetail, params, backcall);
    }

    /**
     * 获取订单详情
     */
    public void getOrderdetial(String token, String id,
                               JsonHttpResponseHandler backcall) {
        RequestParams params = new RequestParams();
        params.put("token", token);
        params.put("id", id);
        Log.e("lh", "url==" + Url.ServerUrl + Url.orderdetial);
        client.post(context, Url.ServerUrl + Url.orderdetial, params, backcall);
    }

    /**
     * 取餐订单详情
     */
    public void getQuCanDetail(String token, String orderno, String dayno, String orderid,
                               JsonHttpResponseHandler backcall) {
        RequestParams params = new RequestParams();
        params.put("token", token);
        params.put("orderno", orderno);
        params.put("dayno", dayno);
        params.put("orderid", orderid);
        Log.e("lh", "url==" + Url.getDetail);
        client.post(context, Url.ServerUrlQT + Url.getDetail, params, backcall);
    }

    /**
     * 确认收货
     */
    public void updateOrder(String token, String orderid, String state,
                            JsonHttpResponseHandler backcall) {
        RequestParams params = new RequestParams();
        params.put("token", token);
        params.put("orderid", orderid);
        params.put("state", state);
        Log.e("WZY", "url==" + Url.ServerUrlQT);
        Log.e("lh", "url==" + Url.newupdateOrder);
        client.post(context, Url.ServerUrlQT + Url.newupdateOrder, params, backcall);
    }

    /**
     * 获取入库单列表
     */
    public void getRukuorders(String token, String page, String offset,
                              JsonHttpResponseHandler backcall) {
        RequestParams params = new RequestParams();
        params.put("token", token);
        params.put("page", page);
        params.put("offset", offset);
        Log.e("lh", "url==" + Url.ServerUrl + Url.rukuorder);
        client.post(context, Url.ServerUrl + Url.rukuorder, params, backcall);
    }

    /**
     * 获取入库单详情
     */
    public void getRukurderdetial(String token, String id,
                                  JsonHttpResponseHandler backcall) {
        RequestParams params = new RequestParams();
        params.put("token", token);
        params.put("id", id);
        Log.e("lh", "url==" + Url.ServerUrl + Url.rukuorderdetial);
        client.post(context, Url.ServerUrl + Url.rukuorderdetial, params, backcall);
    }

    /**
     * 盘点保存
     */
    public void pandiansave(String token, String goods,
                            JsonHttpResponseHandler backcall) {
        RequestParams params = new RequestParams();
        params.put("token", token);
        params.put("goods", goods);
        Log.e("lh", "url==" + Url.ServerUrl + Url.pandiansave);
        client.post(context, Url.ServerUrl + Url.pandiansave, params, backcall);
    }

    /**
     * 盘点提交
     */
    public void pandianconfirm(String token, String id,
                               JsonHttpResponseHandler backcall) {
        RequestParams params = new RequestParams();
        params.put("token", token);
        params.put("id", id);
        Log.e("lh", "url==" + Url.ServerUrl + Url.pandianconfirm);
        client.post(context, Url.ServerUrl + Url.pandianconfirm, params, backcall);
    }

    /**
     * 工作人员信息
     */
    public void info(String token,
                     JsonHttpResponseHandler backcall) {
        RequestParams params = new RequestParams();
        params.put("token", token);
        Log.e("lh", "url==" + Url.ServerUrlQT + Url.info);
        client.post(context, Url.ServerUrlQT + Url.info, params, backcall);
    }

    /**
     * 退款
     */
    public void refund(String token, String id, String object,
                       JsonHttpResponseHandler backcall) {
        RequestParams params = new RequestParams();
        params.put("token", token);
        params.put("id", id);
        params.put("goodsids", object);
        Log.e("lh", "url==" + Url.ServerUrl + Url.refund);
        client.post(context, Url.ServerUrl + Url.refund, params, backcall);
    }

    /**
     * 获取所有商品
     */
    public void backupGetGoods(String token, JsonHttpResponseHandler backcall) {
        RequestParams params = new RequestParams();
        params.put("token", token);
        Log.e("lh", "url==" + Url.ServerUrlQT + Url.backupGetGoods);
        client.post(context, Url.ServerUrlQT + Url.backupGetGoods, params, backcall);
    }

    /**
     * 离线状态下提交购买记录
     */
    public void backupdealCorder(String token, String data, int status,
                                 JsonHttpResponseHandler backcall) {
        RequestParams params = new RequestParams();
        params.put("status", status);
        params.put("token", token);
        params.put("data", data);
        Log.e("lh", "url==" + Url.ServerUrlQT + Url.backupdealCorder);
        client.post(context, Url.ServerUrlQT + Url.backupdealCorder, params, backcall);
    }

    /**
     * 通过手机号获取卡号
     */
    public void phoneCheck(String token, String phone, JsonHttpResponseHandler backcall) {
        RequestParams params = new RequestParams();
        params.put("token", token);
        params.put("phone", phone);
        Log.e("lh", "url==" + Url.ServerUrlQT+Url.phoneCheck);
        client.post(context, Url.ServerUrlQT + Url.phoneCheck, params, backcall);
    }

}
